﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Security;
using Host.Helper;

namespace Host.Models
{
    [Serializable]
    public class ClientModel
    {
        #region Properties
        /// <summary>
        /// The identifier in the datastore
        /// </summary>
        public int ClientId { get; set; }

        /// <summary>
        /// Description used for client object
        /// </summary>
        [DisplayName("Client Name")]
        public string ClientName { get; set; }

        /// <summary>
        /// Physical address for the client
        /// </summary>
        [DisplayName("Physical Address")]
        public string PhysicalAddress { get; set; }

        /// <summary>
        /// Postal address for the client
        /// </summary>
        [DisplayName("Postal Address")]
        public string PostalAddress { get; set; }

        /// <summary>
        /// The set whether the client is active/not active
        /// </summary>
        [DisplayName("Active")]
        public bool Active { get; set; }

        /// <summary>
        /// Date the client was created
        /// </summary>
        [DisplayName("Date Created")]
        public DateTime DateCreated { get; set; }

        /// <summary>
        /// The user that cteated the client
        /// </summary>
        [DisplayName("Created By")]
        public string CreatedBy { get; set; }

        /// <summary>
        /// The date the client record was modified
        /// </summary>
        [DisplayName("Modified Created")]
        public DateTime ModifiedCreated { get; set; }

        /// <summary>
        /// The user that modified the client record
        /// </summary>
        [DisplayName("Modified By")]
        public string ModifiedBy { get; set; }

        /// <summary>
        /// The clients in the data store
        /// </summary>
        public IEnumerable<Client> Clients { get; set; }

        /// <summary>
        /// The number used for the items in the grids
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// The indicator for the grids
        /// </summary>
        public int PageNumber { get; set; }

        /// <summary>
        /// The total number of clients in the datastore
        /// </summary>
        public int TotalRecords { get; set; }

        /// <summary>
        /// The indicator used for updating the records
        /// </summary>
        public bool Update { get; set; }
        #endregion

        #region Methods
        /// <summary>
        /// Get the ClientModel with Values [Clients]
        /// </summary>
        /// <param name="startPage" type="int">Start Page value</param>
        /// <param name="pageSize" type="int">Number of items in the Page [GridPage]</param>
        /// <returns type="ClientModel">ClientModel Object</returns>
        public ClientModel GetData(int startPage, int pageSize)
        {
            using (Data db = new Data())
            {
                var unclean = db.Client.OrderBy(i => i.ClientName).Skip(startPage * pageSize).Take(pageSize).ToList();

               // var clean = unclean.ForEach(x => x.ClientName.Replace(",", " ").Replace(".", " ").ToList());
                var clean = new List<Host.Models.Client>();

                foreach (var item in unclean)
                {

                    clean.Add(new Host.Models.Client
                    {
                        PhysicalAddress = item.PhysicalAddress.Replace(",", " ").Replace(".", " ").Replace(Environment.NewLine, " "),
                        PostalAddress = item.PostalAddress.Replace(",", " ").Replace(".", " ").Replace(Environment.NewLine, " "),
                        ModifiedBy = item.ModifiedBy,
                        DateModified = item.DateModified,
                        ClientName = item.ClientName,
                        ClientId = item.ClientId,
                        Active = item.Active
                    });
                }

               return  new ClientModel() { 
                   //Clients = db.Client.OrderBy(i=> i.ClientName).Skip(startPage * pageSize).Take(pageSize).ToList(),
                   Clients = clean,
                   PageSize = pageSize, 
                   PageNumber = startPage,
                   TotalRecords = db.Client.ToList().Count 
            };  
            }
        }

        /// <summary>
        /// Update the Client Object in the datastore
        /// </summary>
        /// <param name="c" type="ClientModel">ClientModel object</param>
        public void UpdateClient(ClientModel c)
        {
            
            try
            {
                using(Data db = new Data())
                {
                    var dirty = db.Client.Where(i => i.ClientId == c.ClientId).Single();
                    dirty.Active = c.Active;
                    dirty.ClientName = c.ClientName;
                    dirty.DateModified = DateTime.Now;
                    dirty.ModifiedBy = Security.PrincipalUser.LoggedInUser.UserName;
                    dirty.PhysicalAddress = c.PhysicalAddress;
                    dirty.PostalAddress = c.PostalAddress;
                    db.SaveChanges();
                }
            }
            catch (System.Exception ex)
            {
                ExceptionHelper.CreateException(199, ex.Message, (int)ErrorHelper.ErrorTokin.ClientUpdate);//Write the exception into the database
            }
        }

        #endregion
    }

    public class ClientContact
    {

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Client Name")]
        public int ClientId { get; set; }



    }
}